package com.vcg.kafka;

import lombok.extern.java.Log;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.annotation.TopicPartition;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerContainerFactory;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ConcurrentMessageListenerContainer;
import org.springframework.kafka.support.Acknowledgment;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wuyu on 2016/9/20.
 */
@SpringBootApplication
@Log
@EnableKafka
public class KafkaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(KafkaConsumerApplication.class, args);
    }


    @Bean
    public KafkaListenerContainerFactory<ConcurrentMessageListenerContainer<Integer, String>> kafkaListenerContainerFactory() {
        ConcurrentKafkaListenerContainerFactory<Integer, String> factory =
                new ConcurrentKafkaListenerContainerFactory<>();
        factory.setConsumerFactory(consumerFactory());
        factory.setConcurrency(3);
        factory.getContainerProperties().setPollTimeout(3000);
        return factory;
    }

    @Bean
    public ConsumerFactory<Integer, String> consumerFactory() {
        return new DefaultKafkaConsumerFactory<>(consumerConfigs());
    }

    @Bean
    public Map<String, Object> consumerConfigs() {
        Map<String, Object> props = new HashMap<>();
        props.put("bootstrap.servers", "localhost:9092");
        props.put("group.id", "test");
        props.put("auto.commit.interval.ms", "1000");
        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class);
        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        return props;
    }

    @KafkaListener(containerFactory = "kafkaListenerContainerFactory",
            topicPartitions = {@TopicPartition(topic = "test", partitions = {"0"})})
    public void listen(String data) {
        log.info(data);
    }


//    private final ConsumerConnector consumer;
//    private final String topic;
//    private ExecutorService executor;
//
//    public ConsumerDemo(String a_zookeeper, String a_groupId, String a_topic) {
//        consumer = Consumer.createJavaConsumerConnector(createConsumerConfig(a_zookeeper,a_groupId));
//        this.topic = a_topic;
//    }
//
//    public void shutdown() {
//        if (consumer != null)
//            consumer.shutdown();
//        if (executor != null)
//            executor.shutdown();
//    }
//
//    public void run(int numThreads) {
//        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
//        topicCountMap.put(topic, new Integer(numThreads));
//        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer
//                .createMessageStreams(topicCountMap);
//        List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
//
//        // now launch all the threads
//        executor = Executors.newFixedThreadPool(numThreads);
//
//        // now create an object to consume the messages
//        //
//        int threadNumber = 0;
//        for (final KafkaStream stream : streams) {
//            executor.submit(new ConsumerMsgTask(stream, threadNumber));
//            threadNumber++;
//        }
//    }
//
//    private static ConsumerConfig createConsumerConfig(String a_zookeeper,
//                                                       String a_groupId) {
//        Properties props = new Properties();
//        props.put("zookeeper.connect", a_zookeeper);
//        props.put("group.id", a_groupId);
//        props.put("zookeeper.session.timeout.ms", "400");
//        props.put("zookeeper.sync.time.ms", "200");
//        props.put("auto.commit.interval.ms", "1000");
//
//        return new ConsumerConfig(props);
//    }
//
//    public static void main(String[] arg) {
//        String[] args = { "172.168.63.221:2188", "group-1", "page_visits", "12" };
//        String zooKeeper = args[0];
//        String groupId = args[1];
//        String topic = args[2];
//        int threads = Integer.parseInt(args[3]);
//
//        ConsumerDemo demo = new ConsumerDemo(zooKeeper, groupId, topic);
//        demo.run(threads);
//
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException ie) {
//
//        }
//        demo.shutdown();
//    }
}
